<template>
  <div id="app">
    <router-view />
    <!-- 简单的演示界面 -->
    <div class="demo-container" v-if="$route.path === '/'">
      <div class="demo-header">
        <h1>🔐 账号密码管理器</h1>
        <p>安全便捷的多平台账号管理工具</p>
      </div>

      <div class="demo-content">
        <div class="demo-card">
          <h2>系统状态</h2>
          <div class="status-item">
            <span class="status-label">后端服务:</span>
            <span class="status-value" :class="{ 'status-online': backendStatus }">
              {{ backendStatus ? '🟢 在线' : '🔴 离线' }}
            </span>
          </div>
          <div class="status-item">
            <span class="status-label">数据库:</span>
            <span class="status-value status-online">🟢 已连接</span>
          </div>
          <div class="status-item">
            <span class="status-label">前端版本:</span>
            <span class="status-value">v1.0.0</span>
          </div>
        </div>

        <div class="demo-card">
          <h2>功能特性</h2>
          <div class="feature-list">
            <div class="feature-item">✅ 多平台支持 (H5/微信/支付宝/抖音)</div>
            <div class="feature-item">✅ 端到端加密</div>
            <div class="feature-item">✅ 多种加密算法</div>
            <div class="feature-item">✅ 生物识别登录</div>
            <div class="feature-item">✅ 密码生成器</div>
            <div class="feature-item">✅ 分类管理</div>
            <div class="feature-item">✅ 云端同步</div>
            <div class="feature-item">✅ 操作日志</div>
          </div>
        </div>

        <div class="demo-card">
          <h2>API 测试</h2>
          <div class="api-test">
            <button @click="testBackendAPI" :disabled="isLoading" class="test-btn">
              {{ isLoading ? '测试中...' : '测试后端连接' }}
            </button>
            <div v-if="apiResult" class="api-result" :class="{ 'success': apiResult.success }">
              {{ apiResult.message }}
            </div>
          </div>
        </div>

        <div class="demo-card">
          <h2>组件演示</h2>
          <div class="component-demo">
            <p>核心组件已创建完成：</p>
            <ul>
              <li>🔐 主密码输入组件</li>
              <li>📱 登录按钮组件</li>
              <li>📁 分类卡片组件</li>
              <li>📝 账号表单组件</li>
              <li>🎲 密码生成器</li>
              <li>📋 账号列表项</li>
            </ul>
            <div style="margin-top: 16px; display: flex; gap: 12px;">
              <router-link to="/login" class="demo-link">📋 访问登录界面</router-link>
              <router-link to="/dashboard" class="demo-link">🏠 进入仪表板</router-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

// 响应式数据
const backendStatus = ref(false)
const isLoading = ref(false)
const apiResult = ref<{ success: boolean; message: string } | null>(null)

// 页面加载
onMounted(() => {
  checkBackendStatus()
})

// 检查后端状态
const checkBackendStatus = async () => {
  try {
    const response = await fetch('/api/system/health', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    })

    if (response.ok) {
      backendStatus.value = true
    }
  } catch (error) {
    console.log('后端服务未启动或无法连接')
    backendStatus.value = false
  }
}

// 测试后端API
const testBackendAPI = async () => {
  isLoading.value = true
  apiResult.value = null

  try {
    const response = await fetch('/api/system/health', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    })

    if (response.ok) {
      const data = await response.json()
      apiResult.value = {
        success: true,
        message: '✅ 后端API连接成功！' + (data.message ? ` - ${data.message}` : '')
      }
      backendStatus.value = true
    } else {
      apiResult.value = {
        success: false,
        message: `❌ API请求失败 (${response.status})`
      }
    }
  } catch (error) {
    apiResult.value = {
      success: false,
      message: '❌ 无法连接到后端服务，请确保后端服务已启动'
    }
    backendStatus.value = false
  } finally {
    isLoading.value = false
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
}

#app {
  min-height: 100vh;
  color: #333;
}

.demo-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.demo-header {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.demo-header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  font-weight: 600;
}

.demo-header p {
  font-size: 1.1rem;
  opacity: 0.9;
}

.demo-content {
  display: grid;
  gap: 20px;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
}

.demo-card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.demo-card h2 {
  font-size: 1.3rem;
  margin-bottom: 16px;
  color: #1a1a1a;
  font-weight: 600;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.status-item:last-child {
  border-bottom: none;
}

.status-label {
  font-weight: 500;
  color: #666;
}

.status-value {
  font-weight: 600;
}

.status-online {
  color: #52c41a;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.feature-item {
  padding: 8px 12px;
  background: #f8f9fa;
  border-radius: 8px;
  font-size: 0.9rem;
}

.api-test {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.test-btn {
  padding: 12px 24px;
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.test-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.test-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}

.api-result {
  padding: 12px;
  border-radius: 8px;
  font-weight: 500;
  background: #fff2f0;
  color: #ff4d4f;
  border: 1px solid #ffccc7;
}

.api-result.success {
  background: #f6ffed;
  color: #52c41a;
  border-color: #b7eb8f;
}

.component-demo ul {
  margin-left: 20px;
  line-height: 1.8;
}

.component-demo li {
  margin-bottom: 4px;
}

.demo-link {
  display: inline-block;
  padding: 8px 16px;
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  color: white;
  text-decoration: none;
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.2s;
}

.demo-link:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

@media (max-width: 768px) {
  .demo-content {
    grid-template-columns: 1fr;
  }

  .demo-header h1 {
    font-size: 2rem;
  }

  .demo-container {
    padding: 15px;
  }
}
</style>